package com.ruoyi.yl.rocketmq.consumer;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
@RocketMQMessageListener(
    topic = "test-topic",
    consumerGroup = "yl-consumer-test-group",
    selectorExpression = "*"  // 控制tag消费 在后面拼接  消费所有Tag的消息
)
public class MessageConsumer implements RocketMQListener<MessageExt> {


    // ✅ 注入你自己的 Bean
//    private final MyService myService;
//
//    public MessageConsumer(MyService myService) {
//        this.myService = myService;
//    }

    @Override
    public void onMessage(MessageExt messageExt) {
        String messageBody = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        String param1 = messageExt.getProperty("key1"); // 获取header中的参数
        String param2 = messageExt.getProperty("key2");
        String tags = messageExt.getTags();

        System.out.println("Received message: " + messageBody);
        System.out.println("Param1: " + param1);
        System.out.println("Param2: " + param2);
        System.out.println("tags: " + tags);
        // 处理业务逻辑
    }
}